<?php

class bannerComponents extends sfComponents
{
	public function executePreviewlist()
    {
   		// use configuration, if limit is not set
        if(empty($this->limit))
        {
            $this->limit = sfConfig::get('mod_banner_limit', 1);
        }
        $c = new Criteria();
        $c->add(BannerItemPeer::BANNER_TYPE, $this->banner_type);
        if ($this->obj_id)
        {
            $c->add(BannerItemPeer::OBJ_ID, $this->obj_id);
        }
        //$c->add(BannerItemPeer::OBJ_ID, 0);
        $c->add(BannerItemPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $c->setLimit($this->limit);
        
        $this->banners = BannerItemPeer::doSelect($c);
/*        foreach ( $this->banners as $updatebanner )
        {
        	$total_views = $updatebanner->getTotalViews();
        	$updatebanner->setTotalViews(++$total_views);
        	$updatebanner->save();
        }*/
		for ($i = 0; $i <count($this->banners); $i++ )
		{
	            $stat = new BannerStat();
	            $stat->setObjType(MyConstants::TYPE_TEXTMODULE);
	            $stat->setObjId($this->obj_id);
	            $stat->setBannerId($this->banners[$i]->getId());
	            $stat->setUserId($this->getUser()->getAttribute('userId'));
	            $stat->setUri($_SERVER['REQUEST_URI']);
	            $stat->save();
		}
        
        
    }
	public function executePreview()
    {
        $c = new Criteria();
        $c->add(BannerItemPeer::BANNER_TYPE, $this->banner_type);
        $c->add(BannerItemPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $c->setLimit(1);
        
        $this->banner = BannerItemPeer::doSelect($c);
        if (!empty($this->banner))
        {        
            $stat = new BannerStat();
            $stat->setObjType(MyConstants::TYPE_TEXTMODULE);
            $stat->setObjId($this->obj_id);
            $stat->setBannerId($this->banner[0]->getId());
            $stat->setUserId($this->getUser()->getAttribute('userId'));
            $stat->setUri($_SERVER['REQUEST_URI']);
            $stat->save();
        }
        
    }
}